package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import MessagePacks.*;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class DetermineCommittee extends IS_dep_manager{
	
	private MainGUI mainGui;
	private JButton btnApply;
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private JComboBox comboBox_2;
	public DifferentUsersPack difThanChairmanPck;
	public DifferentUsersPack difThanMem1Pck;
	public DifferentUsersPack difThanMem2Pck;
	public getCommitteePack gCommPck;
	public String chairmanID;
	public String comitMem1ID;
	public String comitMem2ID;
	public HashMap<String,String> chairmanhMap;
	public HashMap<String,String> mem1hMap;
	public HashMap<String,String> mem2hMap;
	private static final long serialVersionUID = 1L;
	
	public DetermineCommittee(MainGUI mainGui){
		super(mainGui);
		this.mainGui=mainGui;

		this.setSize(782, 610);
		getContentPane().setLayout(null);
		
		getCurrentCommitteeIds();
		DifThanChairma();
		JPanel panel = new JPanel();
		panel.setBounds(212, 42, 450, 32);
		//getContentPane().add(panel);
		getContentPane().add(panel);
		
		JLabel lblDetermineCommittee = new JLabel("Determine Committee");
		lblDetermineCommittee.setFont(new Font("Dialog", Font.BOLD, 16));
		panel.add(lblDetermineCommittee);
		
		JLabel lblSelectChairman = new JLabel("Select Chairman");
		lblSelectChairman.setVerticalAlignment(SwingConstants.TOP);
		lblSelectChairman.setHorizontalAlignment(SwingConstants.LEFT);
		lblSelectChairman.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblSelectChairman.setBounds(220, 118, 180, 19);
		getContentPane().add(lblSelectChairman);
		
		
		
		comboBox = new JComboBox<Object>();
		comboBox.setBounds(418, 117, 129, 20);
		Set mapSet1 = (Set) chairmanhMap.entrySet();
		Iterator mapIterator1 = mapSet1.iterator();
			while(mapIterator1.hasNext()){
			Map.Entry<String, String> mapEntry1 = (Map.Entry) mapIterator1.next();
			String name = new String((String)mapEntry1.getValue()+" - "+(String) mapEntry1.getKey());
			comboBox.addItem(name);
			}
			getContentPane().add(comboBox);
		
		System.out.println("combo1ok");
			
		JLabel lblSelectFirstMember = new JLabel("Select New First Member");
		lblSelectFirstMember.setHorizontalAlignment(SwingConstants.LEFT);
		lblSelectFirstMember.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblSelectFirstMember.setBounds(220, 168, 180, 19);
		getContentPane().add(lblSelectFirstMember);
		
		difThanMem1Pck = new DifferentUsersPack(Integer.parseInt(comitMem1ID),4);
		mainGui.client.handleMessageFromClientUI(difThanMem1Pck);
		System.out.println("return from handleMessageFromClientUI mem1 OK");
		mem1hMap = ((DifferentUsersPack)(mainGui.client.getMessage())).getHashMap();
		System.out.println("mem1ok");
		
		comboBox_1 = new JComboBox<Object>();
		comboBox_1.setBounds(418, 167, 129, 20);
		Set mapSet2 = (Set) mem1hMap.entrySet();
		Iterator mapIterator2 = mapSet2.iterator();
			while(mapIterator2.hasNext()){
			Map.Entry<String, String> mapEntry2 = (Map.Entry) mapIterator2.next();
			String name = new String((String)mapEntry2.getValue()+" - "+(String) mapEntry2.getKey());
			comboBox_1.addItem(name);
			}
		getContentPane().add(comboBox_1);
		
		System.out.println("combo2ok");
		
		JLabel lblSelectSecondMember = new JLabel("Select New Second Member");
		lblSelectSecondMember.setHorizontalAlignment(SwingConstants.LEFT);
		lblSelectSecondMember.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblSelectSecondMember.setBounds(220, 217, 209, 19);
		getContentPane().add(lblSelectSecondMember);
		
		difThanMem2Pck = new DifferentUsersPack(Integer.parseInt(comitMem2ID),4);
		mainGui.client.handleMessageFromClientUI(difThanMem2Pck);
		System.out.println("return from handleMessageFromClientUI mem2 OK");
		mem2hMap = ((DifferentUsersPack)(mainGui.client.getMessage())).getHashMap();
		System.out.println("mem2ok");
		
		comboBox_2 = new JComboBox<Object>();
		comboBox_2.setBounds(418, 216, 129, 20);
		Set mapSet3 = (Set) mem2hMap.entrySet();
		Iterator mapIterator3 = mapSet3.iterator();
			while(mapIterator3.hasNext()){
			Map.Entry<String, String> mapEntry3 = (Map.Entry) mapIterator3.next();
			String name = new String((String)mapEntry3.getValue()+" - "+(String) mapEntry3.getKey());
			comboBox_2.addItem(name);
			}
		getContentPane().add(comboBox_2);
		
		System.out.println("combo3ok");
		setBtnApply(this);
	}
	
	public JButton getBtnApply() {
		return btnApply;
	}
	
	public void setBtnApply(final DetermineCommittee DC)
	{
		ChangeComMember CCM;
		JButton btnApplyChairman = new JButton("Apply");
		btnApplyChairman.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			close();
			IS_dep_manager ManagerMenu = new IS_dep_manager(mainGui);
			ManagerMenu.setVisible(true);
			comboBox.getSelectedItem();
			Set mapSet = (Set) DC.chairmanhMap.entrySet();
			Iterator mapIterator = mapSet.iterator();
				while(mapIterator.hasNext()){
				Map.Entry<String, String> mapEntry = (Map.Entry) mapIterator.next();
				String name = new String((String)mapEntry.getValue()+" - "+(String) mapEntry.getKey());
				if(name.equals((String)comboBox.getSelectedItem()))
						{
						ChangeComMember CCM = new ChangeComMember(Integer.parseInt((String) mapEntry.getKey()),"Chairman");
						DC.mainGui.client.handleMessageFromClientUI(CCM);
						if((int)(DC.mainGui.client.getMessage())==1)
						JOptionPane.showMessageDialog(serverGUI.mainframe,"The Chairman Has Been Replaced Successfully");
						else JOptionPane.showMessageDialog(serverGUI.mainframe,"The Chairman Failed To Be Replaced");

						}
				}
				
				
			}
		});
		btnApplyChairman.setBounds(566, 116, 85, 23);
		getContentPane().add(btnApplyChairman);
		
		
		JButton btnApplyMem1 = new JButton("Apply");
		btnApplyMem1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnApplyMem1.setBounds(566, 166, 85, 23);
		getContentPane().add(btnApplyMem1);
		
		
		JButton btnApplyMem2 = new JButton("Apply");
		btnApplyMem2.setBounds(566, 215, 85, 23);
		getContentPane().add(btnApplyMem2);
	}
	
	/**
	 * Method Gets The Current Committee Members Ids From DB And Set Them Is Class Variables
	 */
	private void getCurrentCommitteeIds(){
		/*Request For And Get The Current Committee From DataBase*/
		gCommPck = new getCommitteePack();
		mainGui.client.handleMessageFromClientUI(gCommPck);
		HashMap<String,String> hMap = ((getCommitteePack)(mainGui.client.getMessage())).getMyHashMap();
		
		/*Set Committee Members Id's In Class*/
		Set mapSet = (Set) hMap.entrySet();
		Iterator mapIterator = mapSet.iterator();
		while(mapIterator.hasNext()){
			Map.Entry<String, String> mapEntry = (Map.Entry) mapIterator.next();
			switch((String)mapEntry.getValue()){
			case "Chairman":{
							chairmanID = new String((String) mapEntry.getKey());
							System.out.println("Chaiman - ID:"+(String) mapEntry.getKey()+", "+"ComRole:"+(String) mapEntry.getValue());
							break;
							}
			case "Member1":{
							comitMem1ID = new String((String) mapEntry.getKey());
							System.out.println("Mem1 - ID:"+(String) mapEntry.getKey()+", "+"ComRole:"+(String) mapEntry.getValue());
							break;
							}
			case "Member2":{
							comitMem2ID = new String((String) mapEntry.getKey());
							System.out.println("Mem2 - ID:"+(String) mapEntry.getKey()+", "+"ComRole:"+(String) mapEntry.getValue());
							break;
							}	
				default:System.out.println("ID:"+(String) mapEntry.getKey()+", "+"ComRole:"+(String) mapEntry.getValue()+" - Shouldn't Happen");
			}
		}
	}
	public void DifThanChairma() {
	difThanChairmanPck = new DifferentUsersPack(Integer.parseInt(chairmanID),4);
	mainGui.client.handleMessageFromClientUI(difThanChairmanPck);
	System.out.println("return from handleMessageFromClientUI chair OK");
	chairmanhMap = ((DifferentUsersPack)(mainGui.client.getMessage())).getHashMap();
	System.out.println("chair ok");
	}
}
